/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadastrarTarefa.java
 *
 * Created on 15/08/2009, 10:35:35
 */

package src.view;


import java.util.logging.Level;
import java.util.logging.Logger;
import src.control.*;
import src.dto.*;
import java.util.*;
import java.awt.Event.*;
import java.text.*;
import java.sql.Timestamp;

/**
 *
 * @author camila
 */
public class CadastrarTarefa extends javax.swing.JFrame {

    /** Creates new form CadastrarTarefa */
    public CadastrarTarefa() {

        initComponents();

        Collection<SolicitanteDTO> lista = new ArrayList<SolicitanteDTO>();
        SolicitanteBO exe = new SolicitanteBO();

        Collection<PrioridadeDTO> listaPrio = new ArrayList<PrioridadeDTO>();
        GenericoBO exePrio = new GenericoBO();

        lista = exe.listaSolicitante(UsuarioLogado.getInstance().getId());
        listaPrio = exePrio.lista();

        solicitante.addItem("Selecione..");
        for (Iterator<SolicitanteDTO> it = lista.iterator(); it.hasNext();) {
            SolicitanteDTO solicitanteDTO = it.next();
            solicitante.addItem(solicitanteDTO.getNome());  
        }
        solicitante.addItem(".. Novo Solicitante");
        solicitante.setSelectedIndex(0);

        prioridade.addItem("Selecione..");
        for (Iterator<PrioridadeDTO> it = listaPrio.iterator(); it.hasNext();) {
            PrioridadeDTO prioridadeDTO = it.next();
            prioridade.addItem(prioridadeDTO.getDescricaoPrioridade());
        }
        prioridade.setSelectedItem(0);
        
        String dat = "dd/MM/yyyy hh:mm:ss";
        String data;

        java.util.Date agora = new java.util.Date();
        SimpleDateFormat formata = new SimpleDateFormat(dat);
        data = formata.format(agora);
        dataInicio.setText(data);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        nomeTarefa = new javax.swing.JTextField();
        dataInicio = new javax.swing.JTextField();
        solicitante = new javax.swing.JComboBox();
        prioridade = new javax.swing.JComboBox();
        cadastrar = new javax.swing.JButton();
        cancelar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        descricaoTarefa = new javax.swing.JTextArea();
        aviso = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setBounds(new java.awt.Rectangle(200, 200, 0, 0));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jSeparator1.setName("jSeparator1"); // NOI18N

        jLabel1.setFont(new java.awt.Font("Verdana", 1, 18));
        jLabel1.setText(":: Cadastrar Tarefa ::"); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(new java.awt.Font("Verdana", 1, 12));
        jLabel2.setText("Tarefa");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setFont(new java.awt.Font("Verdana", 1, 12));
        jLabel3.setText("Descrição");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setFont(new java.awt.Font("Verdana", 1, 12));
        jLabel4.setText("Data da Solicitação");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setFont(new java.awt.Font("Verdana", 1, 12));
        jLabel5.setText("Solicitante");
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setFont(new java.awt.Font("Verdana", 1, 12));
        jLabel6.setText("Prioridade");
        jLabel6.setName("jLabel6"); // NOI18N

        nomeTarefa.setFont(new java.awt.Font("Verdana", 0, 10));
        nomeTarefa.setName("nomeTarefa"); // NOI18N

        dataInicio.setFont(new java.awt.Font("Verdana", 0, 10)); // NOI18N
        dataInicio.setName("dataInicio"); // NOI18N

        solicitante.setBackground(new java.awt.Color(255, 255, 255));
        solicitante.setFont(new java.awt.Font("Verdana", 0, 10));
        solicitante.setName("solicitante"); // NOI18N
        solicitante.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                solicitanteActionPerformed(evt);
            }
        });

        prioridade.setBackground(new java.awt.Color(255, 255, 255));
        prioridade.setFont(new java.awt.Font("Verdana", 0, 10));
        prioridade.setName("prioridade"); // NOI18N

        cadastrar.setFont(new java.awt.Font("Verdana", 1, 12));
        cadastrar.setText("Cadastrar");
        cadastrar.setName("cadastrar"); // NOI18N
        cadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cadastrarActionPerformed(evt);
            }
        });

        cancelar.setFont(new java.awt.Font("Verdana", 1, 12));
        cancelar.setText("Cancelar");
        cancelar.setName("cancelar"); // NOI18N
        cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        descricaoTarefa.setColumns(20);
        descricaoTarefa.setLineWrap(true);
        descricaoTarefa.setRows(5);
        descricaoTarefa.setName("descricaoTarefa"); // NOI18N
        jScrollPane1.setViewportView(descricaoTarefa);

        aviso.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
        aviso.setEditable(false);
        aviso.setFont(new java.awt.Font("Dialog", 1, 10));
        aviso.setForeground(new java.awt.Color(255, 0, 51));
        aviso.setBorder(null);
        aviso.setDoubleBuffered(true);
        aviso.setName("aviso"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 440, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nomeTarefa, javax.swing.GroupLayout.DEFAULT_SIZE, 358, Short.MAX_VALUE)
                            .addComponent(solicitante, 0, 358, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 358, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(prioridade, 0, 359, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dataInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(aviso, javax.swing.GroupLayout.DEFAULT_SIZE, 237, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cadastrar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(nomeTarefa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(solicitante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prioridade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dataInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cadastrar)
                    .addComponent(cancelar)
                    .addComponent(aviso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void solicitanteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_solicitanteActionPerformed
        if (solicitante.getSelectedItem() == ".. Novo Solicitante"){
            this.dispose();	//Fecha tela
            new CadastrarSolicitante().setVisible(true);//Abre o tela principal
        }
    }//GEN-LAST:event_solicitanteActionPerformed

    private void cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarActionPerformed
            this.dispose();	//Fecha tela
            new RelacaoTarefas().setVisible(true);//Abre o tela principal
    }//GEN-LAST:event_cancelarActionPerformed


    //Efetuando Cadastro da Tarefa
    private void cadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cadastrarActionPerformed
            //Status 2-Pendente
            UsuarioDTO usuario = new UsuarioDTO();
            StatusDTO status = new StatusDTO();
            TarefaDTO tarefas = new TarefaDTO();

            TarefaBO exe = new TarefaBO();
            GenericoBO prio = new GenericoBO();
            SolicitanteBO bSolic = new SolicitanteBO();

            usuario.setId_usuario(UsuarioLogado.getInstance().getId());
            status.setIdStatus(2);

            tarefas.setNome_tarefa(nomeTarefa.getText().toString());
            tarefas.setDescricao(descricaoTarefa.getText().toString());

            //Formatando Data (Mudar para timestamp - Mudar de string para timestamp ou pegar data atual)

            try {

                String dt = dataInicio.getText().toString();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
                java.util.Date parsedDate = dateFormat.parse(dt);
                java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
                String dataBanco = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss").format(timestamp);
                tarefas.setData_solicitacao(dataBanco);


                //Timestamp data = (Timestamp) new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").parse(dataInicio.getText().toString());
                //String dataBanco = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss").format(data);
                //tarefas.setData_solicitacao(dataBanco);
            } catch (ParseException ex) {
                Logger.getLogger(CadastrarTarefa.class.getName()).log(Level.SEVERE, null, ex);
            }
   
            tarefas.setSolicitante(bSolic.buscarId(solicitante.getSelectedItem().toString()));
            tarefas.setPrioridade(prio.buscarId(prioridade.getSelectedItem().toString()));
            tarefas.setUsuario(usuario);
            tarefas.setStatus(status);

            //aviso.setText(exe.cadastrarTarefa(tarefas));
            //Limpando campos
            nomeTarefa.setText("");
            descricaoTarefa.setText("");
            solicitante.setSelectedIndex(0);
            prioridade.setSelectedIndex(0);
    }//GEN-LAST:event_cadastrarActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        this.dispose();	//Fecha tela
        new RelacaoTarefas().setVisible(true);//Abre o tela principal
        
    }//GEN-LAST:event_formWindowClosing

    /**
    * @param args the command line arguments
    */

    /*public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new CadastrarTarefa().setVisible(true);
    }
    });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField aviso;
    private javax.swing.JButton cadastrar;
    private javax.swing.JButton cancelar;
    private javax.swing.JTextField dataInicio;
    private javax.swing.JTextArea descricaoTarefa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField nomeTarefa;
    private javax.swing.JComboBox prioridade;
    private javax.swing.JComboBox solicitante;
    // End of variables declaration//GEN-END:variables

}
